package com.idconfirmsystem.activity;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.DocumentException;
import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.idconfirmsystem.R;
import com.idconfirmsystem.adapter.BagVerifyQueryAdapter;
import com.idconfirmsystem.model.BagVerifyInfo;
import com.idconfirmsystem.model.BagVerifyInfoList;
import com.idconfirmsystem.model.BagVerifyInfoQuery;
import com.idconfirmsystem.model.QueryRet;
import com.idconfirmsystem.model.UserBean;
import com.idconfirmsystem.remote.ConfirmApi;
import com.idconfirmsystem.util.DateUtil;
import com.idconfirmsystem.util.XmlUtils;

public class BagVerifyQueryActivity extends KJActivity implements
		android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener {

	@BindView(id = R.id.lv_bag)
	private ListView bagListView;
	@BindView(id = R.id.progressbar)
	ProgressBar progressbar;
	@BindView(id = R.id.swipe)
	SwipeRefreshLayout swipe;

	private List<BagVerifyInfo> bVerifyInfos;
	private BagVerifyInfoQuery query;
	private BagVerifyQueryAdapter adapter;
	QueryRet<BagVerifyInfoList> qResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_bag_verify_query);
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		super.initData();
		if (query == null) {
			query = new BagVerifyInfoQuery();
		}
		if (UserBean.getInstance().getBranchNumber() != null) {
			query.setBranchNo(UserBean.getInstance().getBranchNumber());
		} else {
			query.setBranchNo("022900");
		}
	}

	@Override
	public void initDataFromThread() {
		// TODO Auto-generated method stub
		super.initDataFromThread();

		String serviceMethod = "BankService/searchVerifyInfo";
		ConfirmApi.queryData(query, serviceMethod, callBack);
	}

	HttpCallBack callBack = new HttpCallBack() {

		@Override
		public void onFailure(int errorNo, String strMsg) {
			// TODO Auto-generated method stub
			super.onFailure(errorNo, strMsg);
		}

		@Override
		public void onSuccess(String t) {
			// TODO Auto-generated method stub
			super.onSuccess(t);
			try {
				qResult = XmlUtils.toBean(BagVerifyInfoList.class, t);
				updateList();
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	};

	@Override
	public void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();
		bagListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(BagVerifyQueryActivity.this, BagVerifyActivity.class);
				Bundle mbundle = new Bundle();
				mbundle.putSerializable("bag", bVerifyInfos.get(arg2));
				intent.putExtras(mbundle);
				startActivityForResult(intent, 1001);
			}
		});
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	public void updateList() {
		if (qResult.getTotalnum() <= 0) {
			bVerifyInfos = new ArrayList<BagVerifyInfo>();
			Toast.makeText(getApplicationContext(), "未获取到要核验的箱包,暂使用测试数据", Toast.LENGTH_SHORT).show();
			for (int i = 0; i < 30; i++) {// testdata
				BagVerifyInfo itemBagVerifyInfo = new BagVerifyInfo();
				itemBagVerifyInfo.setArrival_time("2015090" + i);
				itemBagVerifyInfo.setBags_number("891000" + i);
				itemBagVerifyInfo.setBranch_number("XY100" + i);
				itemBagVerifyInfo.setCoordinate_title("兴业银行莲坂支行" + i);
				if (i % 2 == 1) {
					itemBagVerifyInfo.setVerifyFlag("0");
				} else {
					itemBagVerifyInfo.setVerifyFlag("1");
					itemBagVerifyInfo.setBags_seal_no(itemBagVerifyInfo.getBags_number() + "1000" + i);
					itemBagVerifyInfo.setVerifyTime(DateUtil.getCurrentDateByFormat("MM月dd日 hhmm"));
				}
				bVerifyInfos.add(itemBagVerifyInfo);
			}
		} else {
			bVerifyInfos = qResult.getResult().getBgInfos();
		}

		if (bVerifyInfos != null) {
			if (adapter == null) {
				adapter = new BagVerifyQueryAdapter(getApplicationContext(), bagListView, bVerifyInfos);
				bagListView.setAdapter(adapter);
			} else {
				adapter.setBaglist(bVerifyInfos);
				adapter.notifyDataSetChanged();
			}
		}
	}

	BagVerifyQueryHandler myHandler = new BagVerifyQueryHandler();

	 class BagVerifyQueryHandler extends Handler {
		public BagVerifyQueryHandler() {
		}

		public BagVerifyQueryHandler(Looper looper) {
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1001:// 加载数据
				BagVerifyQueryActivity.this.updateList();
				break;

			default:
				break;
			}
		}

	}
}
